package cn.zf233.xcloud.config;

import cn.zf233.xcloud.filter.SessionStatusFilter;
import cn.zf233.xcloud.intercept.MaintainIntercept;
import cn.zf233.xcloud.intercept.PermissionCheck;
import cn.zf233.xcloud.intercept.PermissionCheckAdmin;
import cn.zf233.xcloud.service.UserService;
import cn.zf233.xcloud.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Collections;

/**
 * Created by zf233 on 2020/11/4
 * MVC 配置类
 */
@Configuration
public class MVCConfig implements WebMvcConfigurer {

    /*@Override
    public void addViewControllers(ViewControllerRegistry registry) {

        registry.addViewController("/").setViewName("index");
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
    }*/

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private UserService userService;

    @Bean
    public FilterRegistrationBean<SessionStatusFilter> filterRegistrationBean() {
        FilterRegistrationBean<SessionStatusFilter> registrationBean = new FilterRegistrationBean<>();
        SessionStatusFilter sessionStatusFilter = new SessionStatusFilter(redisUtil, userService);
        registrationBean.setFilter(sessionStatusFilter);
        registrationBean.setUrlPatterns(Collections.singleton("/user/*"));
        registrationBean.setOrder(1);
        return registrationBean;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // registry.addInterceptor(new ThreadLocalIntercept()).addPathPatterns("/**");

        String[] addPathPatternsMaintain = {
                "/user/**",
                "/file/**",
                "/admin/**",
                "/share/**",
                "/socket/**"
        };
        registry.addInterceptor(new MaintainIntercept()).addPathPatterns(addPathPatternsMaintain);

        String[] addPathPatterns = {
                "/user/**",
                "/file/**",
                "/share/**",
        };
        String[] excludePathPatterns = {
                "/user/login",
                "/user/login/email",
                "/user/login/phone",
                "/user/login/code/email",
                "/user/login/code/phone",
                "/user/jump/**",
                "/share/get/**"
        };
        registry.addInterceptor(new PermissionCheck()).addPathPatterns(addPathPatterns).excludePathPatterns(excludePathPatterns);

        String[] addPathPatternsAdmin = {
                "/admin/**",
                "/socket/**"
        };
        String[] excludePathPatternsAdmin = {
                "/admin/feedback",
                "/admin/agreement",
                "/admin/app",
                "/admin/starter",
                "/admin/toast/index",
                "/admin/toast/home"
        };

        registry.addInterceptor(new PermissionCheckAdmin()).addPathPatterns(addPathPatternsAdmin).excludePathPatterns(excludePathPatternsAdmin);

        String[] addPathPatternsMobile = {
                "/**",
        };
        String[] excludePathPatternsMobile = {
                "/file/share/get",
        };
        // registry.addInterceptor(new MobileIntercept()).addPathPatterns(addPathPatternsMobile).excludePathPatterns(excludePathPatternsMobile);
    }

    @Bean
    public RestTemplate restTemplate(RestTemplateBuilder builder) {
        return builder.build();
    }
}